
//HashMap数据容器
HashMap = function(){
    this.length = 0;
    this.items = new Array();
    this.keys = new Array();
    for (var i = 0; i < arguments.length; i += 2) {
        if (typeof(arguments[i + 1]) != 'undefined') {
            this.items[arguments[i]] = arguments[i + 1];
            this.length++;
        }
    }
};
HashMap.prototype = {
    get:function(key) {
    	return this.items[key];
    },
    put:function(key, value){
        this.keys.push(key);
	    if (typeof(value) != 'undefined') {
	      if (typeof(this.items[key]) == 'undefined') {
	        this.length++;
	      }
	      this.items[key] = value;
	    }
    	return value;
    },
    remove:function(key){
        var value;
	    if (typeof(this.items[key]) != 'undefined') {
	      this.length--;
	      var value = this.items[key];
	      delete this.items[key];
	    }
	    return value;
    },
    has:function(key){
    	return typeof(this.items[key]) != 'undefined';
    },
    hasKey:function(key){
    	for(var i=0; i<this.keys.length;i++){
    		if(key==this.keys[i]){
    			return true;
    		}
    	}
    	return false;
    },
    getKeys:function(){
    	return this.keys;
	}
};
//隐藏列名的集合
var showField = new HashMap();
//所有字段的集合
var field  = new HashMap();
//每页显示的条数
var perCountOther = "";
//当前页码
var pageOther = 1;
//数据容器
var dataModel = new HashMap();
//选中行的行数
var clickNumber = new HashMap();
//双击的字段
var dblclickField = new HashMap();
//双击事件的方法
var dblclickFunction= new HashMap();
//单击事件的字段
var clickField = new HashMap();
//单击事件的方法
var clickFunction = new HashMap();
//是否允许多选
var isCheckOfTable = new HashMap();
//需要添加链接的字段
var linkField = new HashMap();
//链接的url
var linkUrl = new HashMap();
//链接自定显示名
var linkName = new HashMap();
//行上的单击事件
var rowDblclick = new HashMap();

var headDisplay = new HashMap();

var colDisplay = new HashMap();

var colWidthMap = new HashMap();

var countNumber = new HashMap();

var notLink = new HashMap();

var count = new HashMap();
//ajax table 对象类
var TableRequest = function(id,perCount){
    this.srcid = "";
    clickNumber.put(id,0);
    this.dataList = new Array();
    this.addList = new Array();
    this.updateKey = new Array();
    this.updateModel = new HashMap();
    this.checkedDataModel = new Array();
    this.addModel = new Array();
    this.removeModel = new Array();
    this.removeModelHashMap = new HashMap();
    this.action;
    this.id = id;
    this.maxRowNumber = perCount;
    this.checkedNumber = new Array();
    this.startIndex = 1;
    if(perCount==null){
        this.perCount = 50;
    }else{
        this.perCount = perCount    
    }
    this.endIndex = this.perCount;
    this.index = 0;
    clickNumber.put(this.id,0);
    this.page = 1;
    this.pagination = true;
    this.key="";
    this.count=0;
};
TableRequest.prototype = {
    initParameter:function(initHiddenField,initAllField,initDblclickField,initDblclickFunction,initClickField,initClickFunction,initLinkField,initLinkName,initLinkUrl,initRowDblclick,initHeadDisplay,initColDisplay,initColWidth,initNotLink,initPagination,isCheck){
        field.put(this.id,initHiddenField.split("/"));
        showField.put(this.id,initAllField.split("/"));
        dblclickField.put(this.id,initDblclickField.split("/"));
        dblclickFunction.put(this.id,initDblclickFunction.split("/"));
        clickField.put(this.id,initClickField.split("/"));
        clickFunction.put(this.id,initClickFunction.split("/"));
        linkField.put(this.id,initLinkField.split("/"));
        linkName.put(this.id,initLinkName.split("/"));
        linkUrl.put(this.id,initLinkUrl.split("%"));
        if(isCheck){
            isCheckOfTable.put(this.id,isCheck);
        }else{
            isCheckOfTable.put(this.id,false);
        }
        	this.pagination = initPagination;
        rowDblclick.put(this.id,initRowDblclick);
        disOrEnAbled1(this.pagination,this.id,dataModel.get(this.id),this.page,this.perCount);
        if(initHeadDisplay=="none"){
        	headDisplay.put(this.id,initHeadDisplay);
        }else{
        	headDisplay.put(this.id,"");
        }
        if(initColDisplay){
        	colDisplay.put(this.id,initColDisplay.split("/"));
        }else{
        	colDisplay.put(this.id,"");
        }
        if(initColWidth!=""){
        	colWidthMap.put(this.id,initColWidth.split("/"));
        }
        notLink.put(this.id,initNotLink);
        count.put(this.id,-1);
    },
    //得到请求的url
    getUrl:function(action){
        return action.split("?")[0];
    },
    //得到请求发送的数据参数
    getParamenters:function(startIndex,perCount,action,endIndex){
        return action.split("?")[1] + "&startIndex="+startIndex+"&perCount="+perCount+"&endIndex="+endIndex+"&"+this.id+"_count="+count.get(this.id);
    },
    //发送请求取得数据
    sendRequest:function(startIndex,endIndex,key,method){
        var pars = this.getParamenters(startIndex,this.perCount,this.action,endIndex,this.count);
        var actionSend = this.getUrl(this.action);
        var map = new HashMap();
        map.put("id", this.id);
        map.put("page",this.page);
        map.put("perCount",this.perCount);
        map.put("pagination",this.pagination);
        if(method){
        	map.put("method",method);
        }
        
        if(key){
        	map.put("key",key);
        }
        var ajaxTable = new Ajax.SaturnRequest(
        actionSend,
        {
             method: "post",
             parameters: pars,
             onComplete:this.doResepose,
             args:map
        });
    },
    //ajax回调函数
    doResepose:function(response, op){
        var xmlDOM= response.responseXML;
        var list;
        var id = op.get("id");
        if(op.get("key")){
        	list = getArrayFromXmlDocument(xmlDOM,op.get("key"),id);
        }else{
            list = getArrayFromXmlDocument(xmlDOM,null,id);
        }
        var pagination = op.get("pagination");
        disOrEnAbled(pagination,id,list,op.get("page"),op.get("perCount"),count.get(id));
        dataModel.put(id,list);
        var method = "";
        if(op.get("method")){
        	method = op.get("method");
        }
        if (method != ""){
        	eval(method+"();");
        }     
        doDrawTable(dataModel.get(id),id);
    },
     //请求上一页或下一页的数据
    nextPage:function(number){
		((this.page>=1&&number==1)||(this.page>1&&number==-1))?this.page += number:this.page = 1;
		//计算当下一页的开始索引
		this.startIndex = (this.page-1)*this.perCount + 1;
		this.endIndex = this.startIndex + this.perCount - 1;
		//this.disOrEnAbled(this.id);
		//清空添加和删除记录
		this.removeModel.length=0;
		this.addModel.length=0;
		//发送请求,请求下一页数据
		this.sendRequest(this.startIndex,this.endIndex);
		
    },
    //xml数据<datas><value key=''>value</value>...</datas>转换成HashMap类型的数据
    setXmlToMap:function(xmlDoc){
        var map = new HashMap();
        var root = xmlDoc.documentElement.getElementsByTagName("value");
        map.put("tableid",this.id);
        
        for(var i=0;i<root.length;i++){
            if(root[i].getAttribute("key")!=null && root[i].getAttribute("key")!=""){
                if(root[i].firstChild){
            		map.put(root[i].getAttribute("key"),root[i].firstChild.nodeValue);
            	}else{
            	    map.put(root[i].getAttribute("key"),"");
            	}
            }
        }
        return map;
    },
    isHaveData:function(){
    	if(dataModel.get(this.id).length==0){
    		return false;
    	}else{
    		return true;
    	}
    },
    //添加多条数据
    addMuchData:function(xmlDoc){
    	var list = new Array();
        list = getArrayFromXmlDocument(xmlDoc);
        dataModel.put(this.id,list);
        doDrawTable(dataModel.get(this.id),this.id);
    },
    //添加xml数据
    addDataFromXml:function(xmlDom,list){
        if(xmlDom && list){
            if(dataModel.get(this.id)){
                var oldData = dataModel.get(this.id);
                var addData = new Array();
                var map = this.setXmlToMap(this.getXmlFromString(xmlDom));
                addData.push(map);
	            for(var i=0;i<addData.length;i++){
	                this.addDataFromXmlHelper(addData[i],oldData,list);
	            }
	            doDrawTable(dataModel.get(this.id),this.id);
            }else{
                this.add(xmlDom);
            }
        }else{
            alert("请指定主键或放入数据");
        }
    },
    addDataFromXmlHelper:function(addData,oldData,list){
        for(var j=0;j<list.length;j++){
            var field = list[j];
            for(var k=0;k<oldData.length;k++){
			    if(addData.get(field)==oldData[k].get(field)){return;}
            } 
            dataModel.get(this.id).push(addData);
        }
    },
    //表格增加一行
    add:function(xmlDoc){
        if(xmlDoc){
            //添加到表格的数据记录 addModel
	        var map = this.setXmlToMap(this.getXmlFromString(xmlDoc));
		    this.setAddModel(map);
		    //添加新的记录
		    if(dataModel.get(this.id)){
		        dataModel.get(this.id).push(map);
		    }else{
		        this.dataList.push(map);
		        dataModel.put(this.id,this.dataList);
		    }
	        //添加到页面表格中
	        doDrawTable(dataModel.get(this.id),this.id);
        }
    },
    //更新表格单元格数据
    updateValue:function(key,value){
        var indexOfEvent = clickNumber.get(this.id);
        //取得原数据
        var oldModel = dataModel.get(this.id)[indexOfEvent];
        //var oldValue = oldModel.get(key);
        //对原数据进行修改
        if(value){
	        oldModel.put(key,value);
	        dataModel.get(this.id)[indexOfEvent]=oldModel;
	        //修改显示数据
	        doDrawTable(dataModel.get(this.id),this.id);
        }
        if(this.updateModel.get(indexOfEvent)){
            this.updateModel.put(indexOfEvent,dataModel.get(this.id)[indexOfEvent]);
        }else{
            this.updateModel.put(indexOfEvent,dataModel.get(this.id)[indexOfEvent]);
            this.updateKey.push(indexOfEvent);
        }
    },
    //通过复选更改一行值
    updateValuesCheck:function(xmlDoc){
        var map = this.setXmlToMap(this.getXmlFromString(xmlDoc));
        
        var indexOfEvent = this.checkedNumber[0];
        dataModel.get(this.id)[indexOfEvent] = map;
        $("_"+this.id).innerText = "";
        doDrawTable(dataModel.get(this.id),this.id);
        if(this.updateModel.get(indexOfEvent)){
            this.updateModel.put(indexOfEvent,dataModel.get(this.id)[indexOfEvent]);
        }else{
            this.updateModel.put(indexOfEvent,dataModel.get(this.id)[indexOfEvent]);
            this.updateKey.push(indexOfEvent);
        }
    },
    //更新表格选中行
    updateValues:function(xmlDoc){
        var map = this.setXmlToMap(this.getXmlFromString(xmlDoc));
       
        var indexOfEvent = clickNumber.get(this.id);
        dataModel.get(this.id)[indexOfEvent] = map;
        $("_"+this.id).innerText = "";
        doDrawTable(dataModel.get(this.id),this.id);
        if(this.updateModel.get(indexOfEvent)){
            this.updateModel.put(indexOfEvent,dataModel.get(this.id)[indexOfEvent]);
        }else{
            this.updateModel.put(indexOfEvent,dataModel.get(this.id)[indexOfEvent]);
            this.updateKey.push(indexOfEvent);
        }
    },
    //删除相同属性值行
    removeByKey:function(key,value){
        var dataSet = dataModel.get(this.id);
        for(var i=0; i<dataSet.length; i++){
            if(dataSet[i].get(key)==value){
                dataSet.remove(i);
            }
        }
    },
    //删除表格选中行
    remove:function(){
        var indexOfEvent = clickNumber.get(this.id);
        if(dataModel.get(this.id).length>0){
		    $("_"+this.id).innerText = "";
		    var removeModel = dataModel.get(this.id)[indexOfEvent];
		    this.removeModelHashMap=dataModel.get(this.id)[indexOfEvent];
		    this.setRemoveModel(removeModel);
			dataModel.get(this.id).remove(indexOfEvent);
			if(dataModel.get(this.id).length > 0){
			    doDrawTable(dataModel.get(this.id),this.id);
			}
	    }
    },
    //查询数据,并添加到表格
    selectFromXml:function(string,key){
        var dom = this.getXmlFromString(string);
        var datas = getArrayFromXmlDocument(dom,key);
        doDrawTable(datas,this.id);
    },
    //查询表格数据
    select:function(action,key,method){
        count.put(this.id,-1);
    	this.key=key;
        this.action = action;
        perCountOther = this.perCount;
        this.startIndex = 1;
        this.endIndex = this.perCount + this.startIndex;
        this.page = 1;
        $("_"+this.id).innerText = "";
        this.sendRequest(this.startIndex,this.endIndex,key,method);
        
    },
    //删除所有选种的数据并取得所删除的数据
    removeCheck:function(){
        for(var i=this.checkedNumber.length-1;i>=0;i--){
            $("_"+this.id).innerText = "";
	        if(dataModel.get(this.id).length>0){
			    var removeModel = dataModel.get(this.id)[this.checkedNumber[i]];
			    this.removeModelHashMap=dataModel.get(this.id)[this.checkedNumber[i]];
			    this.setRemoveModel(removeModel);
				dataModel.get(this.id).remove(this.checkedNumber[i]);
		    }
		}
	    if(dataModel.get(this.id).length > 0){
			    doDrawTable(dataModel.get(this.id),this.id);
			    this.addEvent(this.id);
		}
    },
    getCheckChooseValues:function(){
        var doEval = " var chk = document."+this.id+"Form."+this.id+"Check;"; 
        eval(doEval); 
        this.checkedNumber.length=0;
        this.checkedDataModel.length=0;
        if(chk.length){
	        for(var i=0;i<chk.length;i++){   
	            if(chk[i].checked) {   
	                this.checkedNumber.push(chk[i].value);
	            } 
	        }
        }else{
            this.checkedNumber.push(chk.value);
        }
        for(var i=0;i<this.checkedNumber.length;i++){
            this.checkedDataModel.push(dataModel.get(this.id)[this.checkedNumber[i]]);
        }
        return this.checkedDataModel;
    },
    //取得表格字段的值
	getValue:function(key){
	    var indexOfEvent = clickNumber.get(this.id);
	    if(dataModel.get(this.id)){
		    if(dataModel.get(this.id)[indexOfEvent]!=null　&&　dataModel.get(this.id)[indexOfEvent].get(key)!=null){
		        return dataModel.get(this.id)[indexOfEvent].get(key);
		    }else{
		        return "";
		    }
	    }else{
	        return "";
	    }
	},
	//取得选中行的值
	getRowValues:function(){
	    var indexOfEvent = clickNumber.get(this.id);
	    var rowValues = new HashMap();
	    if(dataModel.get(this.id)){
		    if(dataModel.get(this.id)[indexOfEvent]!=null){
			    for(var i=0;i<field.get(this.id).length;i++){
			        if(dataModel.get(this.id)[indexOfEvent].get(field.get(this.id)[i])){
			            rowValues.put(field.get(this.id)[i],dataModel.get(this.id)[indexOfEvent].get(field.get(this.id)[i]));
			        }else{
			            rowValues.put(field.get(this.id)[i],"");
			        }
			    }
			    return rowValues;
		    }else{
		        alert("请选择一条数据");
		        return;
		    }
	    }else{
	        alert("没有可以操作的数据");
	        return;
	    }
	},
	//取得Array形式的更新数据
	getUpdateModel:function(){
	    return this.updateModel;
	},
	setAddModel:function(addModel){
	    this.addModel.push(addModel);
	},
	//取得Array形式的添加数据
	getAddModel:function(){
	    return this.addModel;
	},
	setRemoveModel:function(removeModel){
	    this.removeModel.push(removeModel);
	},
	//取得Array形式的删除数据
	getRemoveModel:function(){
	    return this.removeModel;
	},
	getXmlOfRemoveOfOneRow:function(){
	    return this.getXmlFromHashMap(this.removeModelHashMap);
	},
	//取得xml从map中
	getXmlFromHashMap:function(model){
	    var xmlDocString = "<datas>";
	    xmlDocString += this.getXmlFromHashMapHelper(model);
	    xmlDocString += "</datas>";
	    return xmlDocString;
	},
	//取得一行数据的xml格式
	getXmlOfRowValues:function(){
	    return this.getXmlFromHashMap(this.getRowValues());
	},
	//取得多选数据xml
	getXmlOfAllCheck:function(){
		var doc;
	    if(window.DOMParser){  
			doc = new DOMParser();
		}else if(window.ActiveXObject){  
			doc = new ActiveXObject("Msxml2.DOMDocument");  
		}
		var header = doc.createProcessingInstruction("xml","version='1.0'  encoding='utf-8'");
		doc.appendChild(header);
		
	    var root = doc.createElement("datas");
	    var list = this.creatXmlNode(this.getCheckChooseValues());
	    root.appendChild(list);
	    return root;
	},
	//转换Array到xml:<list key=''><map><value key=''>value</value>...</map>...</list>
	getXmlFromArray:function(model,modelOther){
	    var xmlDocString = "<list key='" + this.id + "'>";
	    if(model){
	        xmlDocString += this.getXmlFromArrayHelper(model);
	    }
	    if(modelOther){
	        xmlDocString += this.getXmlFromArrayHelper(modelOther)
	    }
	    xmlDocString += "</list>";
	    return xmlDocString;
	},
	getXmlFromHashMapHelper:function(model){
		var xmlDocString = "";
		if(model){
		    var keys = field.get(this.id);
		    var key = "";
		    for(var j=0;j<keys.length-1;j++){
			    key = keys[j];
			    xmlDocString += "<value key=\"" + key + "\">";
			    xmlDocString += model.get(key);
			    xmlDocString += "</value>";
		    }
		}
	    return xmlDocString;
	},
	//取得xml形式的删除数据
	getXmlOfRemoveModel:function(){
	    return this.creatXmlNode(this.getRemoveModel());
	},
	//取得xml形式的添加和修改数据
	getXmlOfAddAndUpdateModel:function(){
	    return this.creatXmlNode(this.getAddModel(),this.getUpdateModel());
	},
	//取得更新数据的xml
	getXmlOfUpdateModel:function(){
	    return this.creatUpdateXmlNode(this.getUpdateModel());
	},
	//创建更新数据的xml
	creatUpdateXmlNode:function(updateModel){
	    var doc;
	    if(window.DOMParser){  
			doc = new DOMParser();
		}else if(window.ActiveXObject){  
			doc = new ActiveXObject("Msxml2.DOMDocument");  
		}
		var header = doc.createProcessingInstruction("xml","version='1.0'  encoding='utf-8'");
		doc.appendChild(header);
		
	    var root = doc.createElement("list");
	    var rootKey = doc.createAttribute("key");
	    rootKey.value = this.id;
	    root.setAttributeNode(rootKey);
	    if(updateModel){
	        this.createXmlNodeHelperFromHashMap(updateModel,this.updateKey,doc,root);
	    }
	    return root;
	},
	//创建xml
	creatXmlNode:function(addModel,updateModel){
	    var doc;
	    if(window.DOMParser){  
			doc = new DOMParser();
		}else if(window.ActiveXObject){  
			doc = new ActiveXObject("Msxml2.DOMDocument");  
		}
		var header = doc.createProcessingInstruction("xml","version='1.0'  encoding='utf-8'");
		doc.appendChild(header);
		
	    var root = doc.createElement("list");
	    var rootKey = doc.createAttribute("key");
	    rootKey.value = this.id;
	    root.setAttributeNode(rootKey);
        
	    if(addModel){
	        this.createXmlNodeHelperFromArray(addModel,doc,root);
	    }
	    if(updateModel){
	        this.createXmlNodeHelperFromHashMap(updateModel,this.updateKey,doc,root);
	    }
	    return root;
	},
	createXmlNodeHelperFromArray:function(model,doc,root){
	    for(var i=0;i<model.length;i++){
	        var map = doc.createElement("map");
	        for(var j=0;j<field.get(this.id).length-1;j++){
			    var value = doc.createElement("value");
			    var valueKey = doc.createAttribute("key");
			    valueKey.value = field.get(this.id)[j];
			    value.text = model[i].get(field.get(this.id)[j]);;
			    value.setAttributeNode(valueKey);
			    map.appendChild(value);
		    }
		    root.appendChild(map);
        }
	},
	createXmlNodeHelperFromHashMap:function(model,keys,doc,root){
	    for(var i=0;i<keys.length;i++){
	        var map = doc.createElement("map");
	        for(var j=0;j<field.get(this.id).length-1;j++){
			    var value = doc.createElement("value");
			    var valueKey = doc.createAttribute("key");
			    valueKey.value = field.get(this.id)[j];
			    value.text = model.get(keys[i]).get(field.get(this.id)[j]);;
			    value.setAttributeNode(valueKey);
			    map.appendChild(value);
		    }
		    root.appendChild(map);
        }
	},
	//取得所有数据的xml
	getXmlOfAllData:function(){
	    return this.creatXmlNode(dataModel.get(this.id));
	},
	//取得表格的id
	getKeys:function(){
		return field.get(this.id);
	},
	//将字符串转换成xml
	getXmlFromString:function(str){
	    if(window.DOMParser){  
			var p = new DOMParser();
			return p.parseFromString(str,"text/xml");  
		}else if(window.ActiveXObject){  
			var doc = new ActiveXObject("Msxml2.DOMDocument");  
			doc.loadXML(str); 
			return doc;  
		}else{
			return false;  
		}
	},
	addAll:function(response,op){
         var xmlDOM= response.responseXML;
        var list = getArrayFromXmlDocument(xmlDOM,op.get("key"));
        var id = op.get("id");
        disOrEnAbled(this.pagination,id,list,op.get("page"),op.get("perCount"));
        dataModel.put(id,list);
        doDrawTable(dataModel.get(id),id);
        var items = $("_" + id).getElementsByTagName("tr");
	    for(var i = 0; i < items.length; i++) {
            Event.observe(items[i],"click",function(event){
            var selectedTr = Event.element(event).parentNode;
	        clickNumber.put(id,parseInt(selectedTr.id));
	        bindCursorByIndex(clickNumber.get(id),"_" + id);},false);
        }
        cleanAllCursor("_" + id);
        if(items[clickNumber.get(id)]){
		    items[clickNumber.get(id)].firstChild.className = "table-leftbar-highlight";
		    items[clickNumber.get(id)].firstChild.innerHTML = "4";
	    }else{
	        clickNumber.put(id,clickNumber.get(id)-1);
	        items[clickNumber.get(id)].firstChild.className = "table-leftbar-highlight";
	        items[clickNumber.get(id)].firstChild.innerHTML = "4";
	    }
    },
    getAllData:function(key){
	   var listOfKey = new Array();
	   var datas = dataModel.get(this.id);
	   for(var i = 0;i<datas.length;i++){
	   	    listOfKey.push(datas[i].get(key));
	   }
	   return listOfKey;
	},
	//将文件转换成xml
	getXmlFromFile:function(file){
	    if(window.DOMParser){  
			var p = new DOMParser();
			return p.parseFromString(str,"text/xml");  
		}else if(window.ActiveXObject){  
			var doc   =   new   ActiveXObject("Msxml2.DOMDocument"); 
			doc.load(file); 
			return doc;  
		}else{
			return false;  
		}
	},
	getCount:function(){
		return dataModel.get(this.id).length;
	},
	addDataOfMap:function(map){
		dataModel.get(this.id).push(map);
		doDrawTable(dataModel.get(this.id),this.id);
	},
	removeAll:function(){
		dataModel.get(this.id).length=0;
		doDrawTable(dataModel.get(this.id),this.id);
	},
	toPage:function(toPage){
		var startIndex = (toPage-1)*this.perCount;
		var endIndex = startIndex + this.perCount;
		this.page=toPage;
		//disOrEnAbled(this.pagination,this.id,[],toPage,"50",count.get(this.id));
		this.sendRequest(startIndex,endIndex);
	},
	firstPage:function(){
		var startIndex = 1;
		var endIndex = this.perCount+1;
		//$(this.id+"nowPage").innerHTML=2;
		this.page=1;
		//disOrEnAbled(this.pagination,this.id,[],"1","50",count.get(this.id));
		this.sendRequest(startIndex,endIndex,this.key);
		
	},
	lastPage:function(){
		this.count = count.get(this.id);
	    var page = 0 ;
	    page=count.get(this.id)/this.perCount;
	    this.page=Math.ceil(page);
	    var startIndex=(this.page-1)*this.perCount;
		var endIndex = startIndex + this.perCount;
		//disOrEnAbled(this.pagination,this.id,[],page,"50",count.get(this.id));
		this.sendRequest(startIndex,endIndex,this.key);
    }
};

//把xml转换成map数据
function getMapFromXmlDocument(xmlDOM){
    var root = xmlDOM.documentElement;
    var map = root.getElementsByTagName("map");
    var dataObject = new HashMap();
    for(var j=0;j<map.length;j++){
        var keyOfMap = map[j].getAttribute("key");
        dataObject.put("rowid",keyOfList);
        var data = map[j].getElementsByTagName("value");
        for(var k=0;k<data.length;k++){ 
            var key = data[k].getAttribute("key");
            var value = data[k].firstChild.nodeValue;
            dataObject.put(key,value);
        }
    }
    return dataObject;
}
//解析xml形成Array
function getArrayFromXmlDocument(xmlDOM,key,id){
    var root = xmlDOM.documentElement;
    var countSet = root.getElementsByTagName("value");
    var countKey = key+"_count";
    for(var i=0;i<countSet.length;i++){    
    	if(countSet[i].getAttribute("key")==countKey){
    		count.put(id,countSet[i].firstChild.nodeValue);
    	}
    }
    var list = root.getElementsByTagName("list");
    var dataSet = new Array();
    
    for(var i = 0;i<list.length;i++){
        if(key!=""&&key!=null){
		    if(key==list[i].getAttribute("key")){
		        var map = list[i].getElementsByTagName("map");
		        var keyOfList = list[i].getAttribute("key");
		        for(var j=0;j<map.length;j++){
		        	var dataObject = new HashMap();
		            var keyOfMap = map[j].getAttribute("key");
		            var data = map[j].getElementsByTagName("value");
		            for(var k=0;k<data.length;k++){ 
		                var key = data[k].getAttribute("key");
		                var value = "";
		                if(data[k].firstChild){
		                	value = data[k].firstChild.nodeValue;
		                }
		                dataObject.put(key,value);
		            }
		            dataSet.push(dataObject);
		        }
		    }
		}else{
		    
		    var map = list[0].getElementsByTagName("map");
		    var keyOfList = list[0].getAttribute("key");
		    for(var j=0;j<map.length;j++){
		        var dataObject = new HashMap();
		        var keyOfMap = map[j].getAttribute("key");
		        var data = map[j].getElementsByTagName("value");
		        for(var k=0;k<data.length;k++){ 
		            var key = data[k].getAttribute("key");
		            var value = "";
		            if(data[k].firstChild){
		                value = data[k].firstChild.nodeValue;
		            }
		            dataObject.put(key,value);
		        }
		        dataSet.push(dataObject);
		    }
		}
    }
    list = dataSet;
    return dataSet;
};
//取得table串
function getTableString(dataSet,id){
    var onclickSet = new HashMap();
    var ondblclickSet = new HashMap();
    var linkNameSet = new HashMap();
    var linkUrlSet = new HashMap();
    var linkOpenSet = new HashMap();
    var colWidthSet = new HashMap();
    
    for(var i=0; i<dblclickField.get(id).length-1; i++){
        ondblclickSet.put(dblclickField.get(id)[i],dblclickFunction.get(id)[i]);
    }
    for(var i=0; i<clickField.get(id).length-1; i++){
        onclickSet.put(clickField.get(id)[i],clickFunction.get(id)[i]);
    }
    for(var i=0; i<linkField.get(id).length-1; i++){
        linkNameSet.put(linkField.get(id)[i],linkName.get(id)[i].split(","));
    }    
    if(colWidthMap.get(id)!=null){
    	for(var i=0; i<colWidthMap.get(id).length-1; i++){
    		colWidthSet.put(colWidthMap.get(id)[i].split("\:")[0],colWidthMap.get(id)[i].split("\:")[1]);
    	}
    }
    for(var i=0; i<linkField.get(id).length-1; i++){
    	if(linkUrl.get(id)[i].split("@").length==2){
	        linkUrlSet.put(linkField.get(id)[i],linkUrl.get(id)[i].split("@")[1].split(","));
	        linkOpenSet.put(linkField.get(id)[i],linkUrl.get(id)[i].split("@")[0]);
        }else{
	        linkUrlSet.put(linkField.get(id)[i],linkUrl.get(id)[i].split(","));
        }
    }  
    var keysOfShow = new Array();
    keysOfShow = showField.get(id).slice(0);
    if(headDisplay.get(id) != "none"){
	    for(var j=showField.get(id).length-2; j>=0; j--){
	    	if(!dataSet[0].hasKey(showField.get(id)[j])){
	    		if(colDisplay.get(id)!=""){
	    			var coldis = colDisplay.get(id);
	    			var flag = true;
		    		for(var i=0;i<coldis.length-1;i++){
		    			if(coldis[i]== showField.get(id)[j]){
		    				flag = false;
			    			
		    			} 
		    		}
		    		if(flag){
				    			document.getElementById(id+"_"+showField.get(id)[j]).className="saturn-table-th-dynamic";
				    			keysOfShow.remove(j);
				    		}else{
				    			document.getElementById(id+"_"+showField.get(id)[j]).className="saturn-table-th";
				    		}
	    		}else{
	    			document.getElementById(id+"_"+showField.get(id)[j]).className="saturn-table-th-dynamic";
		    		keysOfShow.remove(j);
	    		}
	    	}else{
	    	    document.getElementById(id+"_"+showField.get(id)[j]).className="saturn-table-th";
	    	}
	    }
    }
    var string = "";
    if(isCheckOfTable.get(id)){
    	string+="<form name=\"" + id + "Form\">";
    }
    for(var i=0; i<dataSet.length; i++){
    	if(rowDblclick.get(id)){
    		string += "<tr ondblclick=\""+rowDblclick.get(id)+"\" onclick=\"eventRow(" + i + ",'" + id + "')\" ";
    	}else{
        	string += "<tr onclick=\"eventRow(" + i + ",'" + id + "')\" ";
        }
        string += (i%2==1?"class='saturn-tr-even' ":"");
        string += "id='" + i + "' ><td class='saturn-table-td' width=\"15\"></td>";
        if(isCheckOfTable.get(id)){
    		string+="<td><input type=\"checkbox\" name=\"" + id + "Check\" value=\"" + i + "\"></td>";
        }
        for(var j=0; j<keysOfShow.length-1; j++){
            var name = i + "" + j;
            string += "<td class='saturn-table-td' ";
            if(colWidthSet.get(keysOfShow[j])){
            	string += " width=\""+colWidthSet.get(keysOfShow[j])+"\"";
            }
            if(onclickSet.get(keysOfShow[j])){
                string += "onclick=\"eventRow(" + i + ",'" + id + "');" + onclickSet.get(keysOfShow[j]) + "\" ";
            }
            if(ondblclickSet.get(keysOfShow[j])){
                string += "ondblclick=\"" + ondblclickSet.get(keysOfShow[j]) + "\" ";
            }
            if(dataSet[i].get(keysOfShow[j])){
            	if(linkUrlSet.get(keysOfShow[j])&&linkNameSet.get(keysOfShow[j])){
            		if(linkOpenSet.get(keysOfShow[j])){
            			string += ">";
            			for(var k=0; k<linkUrlSet.get(keysOfShow[j]).length;k++){
            				var nameValue = dataSet[i].get(keysOfShow[j]);
            				if(linkNameSet.get(keysOfShow[j])[k]){
            					nameValue = linkNameSet.get(keysOfShow[j])[k];	
            				}
            				if(notLink.get(id)!="last"){
	            				string += "<a onclick=\"window.open('" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"','','');return false;\" href=\"" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"\">" +nameValue+ "</a>&nbsp;";
            				}else{
            					if(i==dataSet.length-1){
            						string += nameValue + "&nbsp;";
            					}else{
            						string += "<a onclick=\"window.open('" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"','','');return false;\" href=\"" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"\">" +nameValue+ "</a>&nbsp;";
            					}
            				}
            			}
            			string += "</td>";
            		}else{
            			string += ">";
            			for(var k=0; k< linkUrlSet.get(keysOfShow[j]).length;k++){
            				var nameValue = dataSet[i].get(keysOfShow[j]);
            				if(linkNameSet.get(keysOfShow[j])[k]){
            					nameValue = linkNameSet.get(keysOfShow[j])[k];	
            				}
            				if(notLink.get(id)!="last"){
            					string += "<a href=\"" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"\">" +nameValue+ "</a>&nbsp;";
            				}else{
            					if(i==dataSet.length-1){
            						string += nameValue + "&nbsp;";
            					}else{
            						string += "<a href=\"" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"\">" +nameValue+ "</a>&nbsp;";
            					}
            				}
            			}
            			string += "</td>";
            		}
            	}else if(linkUrlSet.get(keysOfShow[j])&&!linkNameSet.get(keysOfShow[j])){
            		if(linkOpenSet.get(keysOfShow[j])){
            			string += ">";
            			for(var k=0; k< linkUrlSet.get(keysOfShow[j]).length;k++){
            				if(notLink.get(id)!="last"){
            					string += "<a onclick=\"window.open('" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"','','');return false;\" href=\"" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"\">" +dataSet[i].get(keysOfShow[j])+ "</a>&nbsp;";
            				}else{
            					if(i==dataSet.length-1){
            						string += dataSet[i].get(keysOfShow[j]) + "&nbsp;";
            					}else{
            						string += "<a onclick=\"window.open('" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"','','');return false;\" href=\"" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"\">" +dataSet[i].get(keysOfShow[j])+ "</a>&nbsp;";
            					}
            				}
            			}
            			string +="</td>";
            		}else{
            			string += ">";
            			for(var k=0; k<linkUrlSet.get(keysOfShow[j]).length;k++){
            				if(notLink.get(id)!="last"){
            					string += "<a href=\"" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"\">" +dataSet[i].get(keysOfShow[j])+ "</a>&nbsp;";
            				}else{
            					if(i==dataSet.length-1){
            						string += dataSet[i].get(keysOfShow[j]) + "&nbsp;";
            					}else{
            						string += "<a href=\"" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"\">" +dataSet[i].get(keysOfShow[j])+ "</a>&nbsp;";
            					}
            				}
            			}
            			string += "</td>";
            		
            		}
            	}else {
            		string += ">" + dataSet[i].get(keysOfShow[j]) + "</td>"; 
            	}
            }else{
                if(linkUrlSet.get(keysOfShow[j])&&linkNameSet.get(keysOfShow[j])){
            		if(linkOpenSet.get(keysOfShow[j])){
            			string += ">";
            			for(var k=0; k<linkUrlSet.get(keysOfShow[j]).length;k++){
            				if(notLink.get(id)!="last"){
            					string += "<a onclick=\"window.open('" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"','','');return false;\" href=\"" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"\">" +linkNameSet.get(keysOfShow[j])[k]+ "</a>&nbsp;";
            				}else{
            					if(i==dataSet.length-1){
            						string += linkNameSet.get(keysOfShow[j])[k] + "&nbsp;";
            					}else{
            						string += "<a onclick=\"window.open('" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"','','');return false;\" href=\"" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"\">" +linkNameSet.get(keysOfShow[j])[k]+ "</a>&nbsp;";
            					}
            				}
            			}
            			string += "</td>";
            		}else{
            			string += ">";
            			for(var k=0; k< linkUrlSet.get(keysOfShow[j]).length;k++){
            				if(notLink.get(id)!="last"){
            					string += "<a href=\"" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"\">" +linkNameSet.get(keysOfShow[j])[k]+ "</a>&nbsp;";
            				}else{
            					if(i==dataSet.length-1){
            						string += linkNameSet.get(keysOfShow[j])[k] + "&nbsp;";
            					}else{
            						string += "<a href=\"" +getUrl(linkUrlSet.get(keysOfShow[j])[k],dataSet[i])+"\">" +linkNameSet.get(keysOfShow[j])[k]+ "</a>&nbsp;";
            					}
            				}
            			}
            			string += "</td>";
            		}
            	}else {
            		string += "></td>"; 
            	}
            }
        }
        string += "</tr>";
    }
    if(isCheckOfTable.get(id)){
    		string += "</form>";
    }
    return string;
};
function getValue(map,key){
	var value = "";
	if(map.get(key)==null){
		return document.getElementsByName(key)[0].value;
	}
	return map.get(key);
};
function getUrl(url,map){
	var sub_str="";
	if(url.indexOf("#")!=-1){
		var size = url.match(/\#/gi).length;
		for(var i=0;i<size;i++){
			var number = url.indexOf("#");
			var number_last = url.lastIndexOf("#");
			if(i%2!=0){
	    		sub_str += getValue(map,url.substring(0,number));
			}else{
	    		sub_str += url.substring(0,number); 
			}
			url= url.substring(number+1,number_last+1);
		}
	}else{
		sub_str = url;
	}
	return sub_str;
};
function eventRow(i,id){
    clickNumber.put(id,i);
	var cursor = $("_"+id).getElementsByTagName("tr");
	cleanAllCursor("_" + id);
	if(cursor[i]){
	    cursor[i].firstChild.className = "table-leftbar-highlight";
	    cursor[i].firstChild.innerHTML = "4";
	    cursor[i].className = "saturn-dynamic-row";
	}
};
//分页按扭控制代码
function disOrEnAbled(pagination,id,list,page,perCount,count){
	if(pagination){
		page==1?$(id+"cPage").disabled = true:$(id+"cPage").disabled = false;
		(list[0]==null||list.length<perCount)?$(id+"nPage").disabled = true:$(id+"nPage").disabled = false;
		$(id+"nowPage").innerHTML=page;
		if(count!=null && count!=""){
			//if(count%perCount==0){
				$(id+"Count").innerHTML="共" + count + "条";
			//}else{
				//$(id+"Count").innerHTML="共" + count/perCount + 1 + "条";
			//}
		}
		pageOther = page;
	}
}
//分页按扭控制代码2
function disOrEnAbled1(pagination,id,list,page,perCount){
	if(pagination){
		page==1?$(id+"cPage").disabled = true:$(id+"cPage").disabled = false;
		(list)?$(id+"nPage").disabled = false:$(id+"nPage").disabled = true;
		pageOther = page;
	}
}
//显示table
function doDrawTable(dataSet,id){
    if(dataSet){
	    $("_"+id).innerText = "";
	    new Insertion.Bottom("_"+id,getTableString(dataSet,id));
	    var index = clickNumber.get(id);
	    var cursor = $("_"+id).getElementsByTagName("tr");
    	cleanAllCursor("_"+id);
	    if(cursor[index]){
			cursor[index].firstChild.className = "table-leftbar-highlight";
			cursor[index].firstChild.innerHTML = "4";
			cursor[index].className = "saturn-dynamic-row";
		}
    }
    return id;
};
function bindCursorByIndex(index,id) {
    var cursor = $(id).getElementsByTagName("tr");
    cleanAllCursor(id);
    if(cursor[index]){
		cursor[index].firstChild.className = "table-leftbar-highlight";
		cursor[index].firstChild.innerHTML = "4";
		cursor[index].className = "saturn-dynamic-row";
	}
};
function cleanAllCursor(id){
    var items = $(id).getElementsByTagName("tr");
	for(var i = 0; i < items.length; i++) {
		if(items[i]){
	        items[i].firstChild.className = "saturn-dynamic-row";
	        items[i].firstChild.innerHTML = "";
	        items[i].className = "table-leftbar-highlight"
        }
    }
};
//create xml
function getXmlDoc(str){
    if(window.DOMParser){  
        var p = new DOMParser();
	    return p.parseFromString(str,"text/xml");  
	}else if(window.ActiveXObject){  
	    var doc = new ActiveXObject("Msxml2.DOMDocument");  
		doc.loadXML(str); 
		return doc;  
	}else{
		return false;  
	}
};

//Array的remove方法的实现
Array.prototype.remove=function(index){
    if(isNaN(index)||index>this.length){return false;}
    for(var i=0,n=0;i<this.length;i++){
        if(this[i]!=this[index]){
            this[n++]=this[i]
        }
    }
    this.length-=1
};
//将table1的数据放到table2中
function upOrDown(table1,table2){
    if(dataModel.get(table1.id)){
        if(dataModel.get(table1.id)[0]){
		    table1.remove();
		    var datas = table1.getXmlOfRemoveOfOneRow();
		    if(datas){
		        table2.add(datas);
		    }
	    }
    }else{
        alert("表格中没有数据");
    }
};
//把table1的所有数据转移到table2中
function upOrDownOfAll(table1,table2){
    var dataSet = dataModel.get(table1.id);
    if(dataSet[0]){
        var showDataSet = dataModel.get(table2.id).concat(dataSet);
	    doDrawTable(showDataSet,table2.id);
	    table2.addEvent(table2.id)
	    dataModel.put(table2.id,showDataSet);
	    for(var i=dataSet.length-1; i>=0; i--){
	        dataSet.remove(i);
	    }
	    doDrawTable(dataSet,table1.id);
    }else{
        alert("表格中没有数据");
    }
};
function upOrDownOfEach(table1,table2,i){
    if(dataModel.get(table1.id)){
	    var dataSet = dataModel.get(table1.id);
	    dataModel.get(table2.id).push(dataSet[i]);
	    dataSet.remove(i);
	    doDrawTable(dataSet,table1.id);
	    doDrawTable(dataModel.get(table2.id),table2.id);
    }
};

